<!--
 * @author hemengxia
 红包活动---配置页面
-->
<template>
  <section class="red-envelope-activity-manage">
    <div class="manage-title">配置游戏活动</div>
    <section class="manage-main">
      <el-form
        ref="form"
        v-loading="formloading"
        :model="form"
        :rules="rules"
        label-width="250px"
        class="main-form"
      >
        <el-form-item label="访问地址" prop="visitUrl">
          <el-input v-model="form.visitUrl" placeholder="请输入访问地址" />
        </el-form-item>
        <el-form-item
          v-if="redpacketActivityType =='REDPACT2'"
          label="顶部背景图"
          prop="backgroundImageId"
        >
          <image-upload
            :key="uploadImg.imagecropperKey"
            v-model="form.backgroundImageId"
            :preview-id="uploadImg.viewId"
            :forever="true"
            :width="750"
            :view="uploadImg.view"
            :height="340"
            :no-circle="true"
            :no-rotate="false"
            lang-type="zh"
          />
        </el-form-item>
        <el-form-item
          v-if="redpacketActivityType =='REDPACT2'"
          label="底部背景图"
          prop="backgroundTwoImageId"
        >
          <image-upload
            :key="uploadImg.imagecropperKey2"
            v-model="form.backgroundTwoImageId"
            :preview-id="uploadImg.bottomViewId"
            :forever="true"
            :width="750"
            :view="uploadImg.view"
            :height="1108"
            :no-circle="true"
            :no-rotate="false"
            lang-type="zh"
          />
        </el-form-item>
        <el-form-item
          v-if="redpacketActivityType =='REDPACT0'"
          label="背景图"
          prop="backgroundImageId"
        >
          <image-upload
            :key="uploadImg.imagecropperKey"
            v-model="form.backgroundImageId"
            :preview-id="uploadImg.viewId"
            :forever="true"
            :width="750"
            :view="uploadImg.view"
            :height="1220"
            :no-circle="true"
            :no-rotate="false"
            lang-type="zh"
          />
        </el-form-item>
        <el-form-item
          label="背景色"
          prop="backgroundColor"
        >

          <el-color-picker v-model="form.backgroundColor" />
        </el-form-item>
        <el-form-item
          label="展示类型"
          prop="showType"
        >
          <el-radio-group v-model="form.showType">
            <el-radio v-for="(item, index) in typeList" :key="index" :label="item.id">{{ item.name }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="活动信息接口" prop="activityUrl1">
          <el-input v-model="form.activityUrl1" placeholder="请输入活动信息接口" />
        </el-form-item>
        <el-form-item label="抽奖次数接口" prop="activityUrl2">
          <el-input v-model="form.activityUrl2" placeholder="请输入抽奖次数接口" />
        </el-form-item>
        <el-form-item label="抽奖记录接口" prop="activityUrl3">
          <el-input v-model="form.activityUrl3" placeholder="请输入抽奖记录接口" />
        </el-form-item>
        <el-form-item label="保存答题记录接口" prop="activityUrl4">
          <el-input v-model="form.activityUrl4" placeholder="请输入保存答题记录接口" />
        </el-form-item>
        <el-form-item label="抽奖接口" prop="activityUrl5">
          <el-input v-model="form.activityUrl5" placeholder="请输入抽奖接口" />
        </el-form-item>

        <el-form-item label="接口1" prop="activityUrl6">
          <el-input v-model="form.activityUrl6" placeholder="请输入接口1" />
        </el-form-item>
        <el-form-item label="接口2" prop="activityUrl7">
          <el-input v-model="form.activityUrl7" placeholder="请输入接口2" />
        </el-form-item>
        <el-form-item label="接口3" prop="activityUrl8">
          <el-input v-model="form.activityUrl8" placeholder="请输入接口3" />
        </el-form-item>

        <el-form-item label="配置1" prop="param1">
          <el-input v-model.trim="form.param1" placeholder="请输入配置1" />
        </el-form-item>
        <el-form-item label="配置2" prop="param2">
          <el-input v-model.trim="form.param2" placeholder="请输入配置2" />
        </el-form-item>
        <el-form-item label="配置3" prop="param3">
          <el-input v-model.trim="form.param3" placeholder="请输入配置3" />
        </el-form-item>
        <el-form-item label="配置4" prop="param4">
          <el-input
            v-model="form.param4"
            placeholder="请输入配置4"
            @input="clearNoNum($event,'param4')"
          />
        </el-form-item>
        <el-form-item label="配置5" prop="param5">
          <el-date-picker
            v-model="form.param5"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          />
        </el-form-item>

      </el-form>
    </section>

    <div class="manage-footer">
      <el-button style="padding: 8px 30px;" @click="handleCancel">取消</el-button>
      <el-button style="padding: 8px 30px;" type="primary" @click="handleSubmit">确定</el-button>
    </div>

  </section>
</template>
<script>

import {
  update,
  get,
  insert
} from '@/views/gameactivity/api/config'

export default {
  name: 'GameActivityConfig',
  components: {},
  data() {
    return {
      state: 'create',
      formloading: false,
      form: {
        id: '',
        visitUrl: '/guess/index',
        backgroundImageId: '',
        backgroundTwoImageId: '',
        backgroundColor: '#A10A06',
        showType: 0,
        activityUrl6: '',
        activityUrl7: '',
        activityUrl8: '',
        param1: '',
        param2: '',
        param3: '',
        param4: '',
        param5: '',
        activityUrl1: '/API-ENTERPRISE-CUSTOM/custom/game_dangshi/common/getGameActivityInfo',
        activityUrl2: '/API-ENTERPRISE-CUSTOM/custom/game_dangshi/common/getDrawNumber',
        activityUrl3: '/API-ENTERPRISE-CUSTOM/custom/game_dangshi/common/queryDrawRecord',
        activityUrl4: '/API-ENTERPRISE-CUSTOM/custom/game_dangshi/common/answer',
        activityUrl5: '/API-ENTERPRISE-CUSTOM/custom/game_dangshi/common/draw',
        activityType: 'ACTIVITY02'
      },

      rules: {
        visitUrl: [{
          required: true,
          message: '请输入访问地址',
          trigger: 'blur'
        }
        ],

        backgroundImageId: [{
          required: true,
          message: '请设置封面图片',
          trigger: 'blur'
        }],
        time: [{
          required: true,
          message: '请选择活动时间',
          trigger: 'blur'
        }],
        backgroundColor: [{
          required: true,
          message: '请选择背景色',
          trigger: 'blur'
        }],
        activityUrl1: [{
          required: true,
          message: '请输入活动信息接口',
          trigger: 'blur'
        }],
        activityUrl2: [{
          required: true,
          message: '请输入抽奖次数接口',
          trigger: 'blur'
        }],
        activityUrl3: [{
          required: true,
          message: '请输入抽奖记录接口',
          trigger: 'blur'
        }],
        activityUrl4: [{
          required: true,
          message: '请输入保存答题记录接口',
          trigger: 'blur'
        }],
        activityUrl5: [{
          required: true,
          message: '请输入抽奖接口',
          trigger: 'blur'
        }]

      },
      dict: {
        REDPACT: []
      },
      uploadImg: {
        viewId: '',
        bottomViewId: '',
        view: '/API-DOCUMENT/document/file/download?id=',
        imagecropperKey: 0,
        imagecropperKey2: 0
      },
      typeList: [
        {
          name: '展示金额',
          id: 0
        }, {
          name: '展示份数',
          id: 1
        }
      ],
      redpacketActivityType: ''

    }
  },
  created() {
    this.redpacketActivityType = this.$route.query.redpacketActivityType
    if (this.$route.query.i) {
      this.getData(this.$route.query.i)
    } else {
      this.$message({
        showClose: true,
        message: '配置异常！',
        type: 'warning',
        onClose: () => {
          this.$router.back()
        }
      })
    }
  },
  methods: {
    clearNoNum(value, name) {
      this.form[name] =
          ('' + value) // 第一步：转成字符串
            .replace(/[^\d^\.]+/g, '') // 第二步：把不是数字，不是小数点的过滤掉
            .replace(/^0+(\d)/, '$1') // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
            .replace(/^\./, '0.') // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
            .match(/^\d*(\.?\d{0,4})/g)[0] || '' // 第五步：最终匹配得到结果 以数字开头，只有一个小数点，而且小数点后面只能有0到4位小数
    },
    handleSubmit() {
      this.state === 'create' ? this.createData() : this.updateData()
    },

    handleCancel() {
      this.$router.back()
    },

    /*
     * 新增
     * */
    createData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const params = Object.assign({}, this.form)
          return new Promise((resolve, reject) => {
            insert(params).then(() => {
              loading.close()
              this.$message({
                title: '成功',
                message: '配置成功',
                type: 'success',
                duration: 1500
              })
              this.$router.back()
            }).catch(error => {
              reject(error)
              loading.close()
            })
          })
        }
      })
    },

    /**
     * 编辑提交
     */
    updateData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          const params = {
            ...this.form
          }
          return new Promise((resolve, reject) => {
            update(params).then(() => {
              loading.close()
              this.$message({
                title: '成功',
                message: '配置成功',
                type: 'success',
                duration: 2000
              })
              this.$router.back()
            }).catch(error => {
              reject(error)
              loading.close()
            })
          })
        }
      })
    },

    /**
     * 获取详情
     */
    getData(id) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        get({ activityBaseId: id }).then(res => {
          loading.close()
          const data = res.data.data
          if (data && data.id) {
            this.state = 'update'
            for (const key in this.form) {
              this.form[key] = data[key]
            }
            this.uploadImg.viewId = data.backgroundImageId
            this.uploadImg.bottomViewId = data.backgroundTwoImageId
            this.form.showType = this.form.showType ? this.form.showType : 0
          }
          this.form.activityBaseId = this.$route.query.i
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    }
  }
}
</script>
<style lang="scss">

.red-envelope-activity-manage {
  background-color: #fff;
  min-height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden;

  .manage-title {
    position: relative;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    background-color: #ffffff;
    border-bottom: 1px solid #e5e5e5;
    font-family: PingFangSC-Medium;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;

    &:before {
      content: '';
      position: absolute;
      width: 2px;
      @include bg-color($mainColor);
      top: 13px;
      bottom: 13px;
      left: 15px;
    }
  }

  .manage-main {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 52px;
    box-sizing: border-box;
    padding: 0 10px;
    overflow: hidden;

    &::-webkit-scrollbar {
      width: 8px;
      /*对垂直流动条有效*/
      height: 1px;
      /*对水平流动条有效*/
    }

    /** 滚动条轨道*/
    &::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: white;
      border-radius: 3px;
    }

    /** 滚动条里面小方块*/
    &::-webkit-scrollbar-thumb {
      height: 32px;
      border-radius: 5px;
      box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
      background-color: #eaeaea;
    }

    &:hover {
      overflow: auto;
    }
  }

  .main-form {
    width: 1000px !important;
    margin: 0 auto;

    .line {
      text-align: center;
    }

    .form-card {
      margin-top: 20px;

      .el-card__body {
        max-width: 90%;
        margin: 0 auto;
      }
    }

    .coupon-num input {
      text-align: left;
    }

    .select-data {
      display: inline-block;
      background: rgba(246, 246, 246, 1);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      padding: 0 10px;
      margin-right: 5px;
      margin-bottom: 8px;

      i {
        cursor: pointer;
        padding-left: 5px;
      }
    }

    .selectbtn {
      display: inline-block;
      width: 120px;
      background: rgba(255, 255, 255, 1);
      @include border-color(1px, dotted, $mainColor);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      @include font-color($mainColor);
      text-align: center;
      cursor: pointer;
      margin-bottom: 10px;
    }

    .tip {
      @include font-color($mainColor);
      font-size: 14px;
      padding-left: 250px;
      margin-top: 0;
    }

    .join-conditions-content {
      border: 1px dashed #eee;
      padding: 10px;
      margin-bottom: 20px;

      .title {
        vertical-align: middle;
        font-size: 16px;
        color: #606266;
        line-height: 40px;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        margin: 0;
        font-weight: 700;
      }
    }

  }

  .manage-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    text-align: center;
    padding: 10px 0;
    background-color: white;
    box-shadow: 0 -3px 6px #f5f5f5;
  }
}
</style>
